package com.ebt.m.proposal_v2.mvp.presenter;

import android.content.Context;

import com.ebt.m.proposal_v2.bean.InsuranceEntity;
import com.ebt.m.proposal_v2.bean.event.MainChooseEvent;
import com.ebt.m.proposal_v2.dao.ProposalApi;
import com.ebt.m.proposal_v2.dao.request.ParamGetRisks;
import com.ebt.m.proposal_v2.dao.response.ResponseMain;
import com.ebt.m.proposal_v2.dao.response.ResponseMainDetail;
import com.ebt.m.proposal_v2.dao.response.TouDanInfo;
import com.ebt.m.proposal_v2.mvp.base.BasePresenter;
import com.ebt.m.proposal_v2.mvp.contract.CommonProductContract;
import com.ebt.m.proposal_v2.retrofit.OnResponseListener;
import com.ebt.m.proposal_v2.retrofit.ResponseError;
import com.ebt.m.proposal_v2.retrofit.ResponsePage;
import com.ebt.m.proposal_v2.utils.DataUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * @author archer.qi
 * Created on 2017/5/24.
 */
public class CommonProductPresenter extends BasePresenter<CommonProductContract.View> implements CommonProductContract.Presenter {

    private List<ResponseMain> listData;
    private TouDanInfo touDanInfo;

    public CommonProductPresenter(Context context, CommonProductContract.View view) {
        super(context, view);
    }

    @Override
    public void start() {
        mView.showProgress();
        getCommonProducts(mView.getArgument());
    }

    @Override
    public void getCommonProducts(ParamGetRisks params) {
        params.pageSize = "10"; // 常用产品最多为10
        ProposalApi.getInstance(mContext).getMainProducts(params, new OnResponseListener<List<ResponseMain>>() {
            @Override
            public void onSuccess(List<ResponseMain> body, ResponsePage page) {
                mView.cancelProgress();
                if (body == null || body.size() == 0) {
                    mView.showEmpty(true, false, false);
                    return;
                }
                mView.showEmpty(false, false, false);
                listData = body;
                mView.bindRecyclerData(listData);
            }

            @Override
            public void onFailure(ResponseError error) {
                mView.cancelProgress();
                if (error != null && "-102".equals(error.code)) {
                    mView.showEmpty(false, true, false);
                } else {
                    mView.showEmpty(false, false, true);
                }
            }
        }, mView);
    }

    @Override
    public void getMainProductDetails(int productId, String age, int sex, int profession) {
        ProposalApi.getInstance(mContext).getMainDetails(productId, age, sex, profession, new OnResponseListener<ResponseMainDetail>() {
            @Override
            public void onSuccess(ResponseMainDetail body, ResponsePage page) {
                if (body == null) {
                    submitMainChoose(null);
                    return;
                }
                body.parseOptions();
                submitMainChoose(DataUtils.toInsuranceList(body));
            }

            @Override
            public void onFailure(ResponseError error) {
                submitMainChoose(null);
            }
        }, mView);
    }

    @Override
    public void onItemClick(int position, ParamGetRisks param) {
        if (listData == null || listData.size() == 0) {
            submitMainChoose(null);
            return;
        }
        if (position < 0 || position > listData.size() - 1) {
            submitMainChoose(null);
            return;
        }
        ResponseMain data = listData.get(position);
        if (data == null) {
            submitMainChoose(null);
            return;
        }
        touDanInfo = new TouDanInfo(data.isOpenInsurance, data.insuranceUrl, data.productId);
        if (param != null) {
            /*点击之后，请求主险参数*/
            getMainProductDetails(data.productId, DataUtils.formatRequestParamAge(param.age + "", param.birthday), param.sex, param.profession);
        } else {
            getMainProductDetails(data.productId, DataUtils.formatRequestParamAge("30", ""), 1, 1);
        }
    }

    @Override
    public void submitMainChoose(List<InsuranceEntity> list) {
        MainChooseEvent event = new MainChooseEvent();
        event.list = list;
        event.touDanInfo = new TouDanInfo(touDanInfo);
        touDanInfo = null;
        EventBus.getDefault().post(event);
        mView.finishActivity();
    }


}
